package com.haoxin.ai_course.modules.course.controller.admin;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haoxin.ai_course.common.annotation.SysLog;
import com.haoxin.ai_course.common.base.AbstractController;
import com.haoxin.ai_course.common.utils.DateUtil;
import com.haoxin.ai_course.common.utils.EasyExcelUtil;
import com.haoxin.ai_course.common.utils.R;
import com.haoxin.ai_course.modules.course.entity.AcStatisticsUser;
import com.haoxin.ai_course.modules.course.entity.VO.AcStatisticsUserExcelVO;
import com.haoxin.ai_course.modules.course.service.AcStatisticsUserService;

import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;



/**
 * 用户统计表
 * 
 * @author xingxiangguo
 * @email bugcn@qq.com
 * @date 2022-06-06 09:44:49
 */
@RestController
@AllArgsConstructor
@RequestMapping("/course/acstatisticsuser")
@Api(value="api",tags="后台-用户统计管理")

public class AcStatisticsUserController  extends AbstractController {
    private final  AcStatisticsUserService acStatisticsUserService;
    private final  MapperFacade mapperFacade;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @ApiOperation("用户统计列表")
    @RequiresPermissions("course:acstatisticsuser:list")
    public R list(@RequestParam Map<String, Object> params,@ApiParam("课程编号")@RequestParam(name="courseId",required = false) Integer courseId,@ApiParam("手机号")@RequestParam(name="mobile",required = false) String mobile){
        //查询列表数据
        QueryWrapper<AcStatisticsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AcStatisticsUser::getRevisitTimes, 0);
        queryWrapper.lambda().orderByAsc(AcStatisticsUser::getCourseId);
        if(courseId != null) {
        	queryWrapper.lambda().eq(AcStatisticsUser::getCourseId, courseId);
        }
        if(StringUtils.isNotBlank(mobile)) {
        	queryWrapper.lambda().eq(AcStatisticsUser::getMobile,mobile);
        }
        IPage<AcStatisticsUser> sysConfigList = acStatisticsUserService.page(mpPageConvert.<AcStatisticsUser>pageParamConvert(params),queryWrapper);
        return R.ok().put("page", mpPageConvert.pageValueConvert(sysConfigList));
    }
    /**
     * 列表
     */
    @RequestMapping("/detailList")
    @ApiOperation("用户统计列表")
    @RequiresPermissions("course:acstatisticsuser:list")
    public R detailList(@RequestParam Map<String, Object> params,@ApiParam("课程编号")@RequestParam(name="courseId",required = false) Integer courseId,@ApiParam("手机号")@RequestParam(name="mobile",required = false) String mobile){
    	//查询列表数据
    	QueryWrapper<AcStatisticsUser> queryWrapper = new QueryWrapper<>();
    	if(courseId != null) {
    		queryWrapper.lambda().eq(AcStatisticsUser::getCourseId, courseId);
    	}
    	if(StringUtils.isNotBlank(mobile)) {
    		queryWrapper.lambda().eq(AcStatisticsUser::getMobile,mobile);
    	}
    	  queryWrapper.lambda().orderByAsc(AcStatisticsUser::getCourseId);
    	IPage<AcStatisticsUser> sysConfigList = acStatisticsUserService.page(mpPageConvert.<AcStatisticsUser>pageParamConvert(params),queryWrapper);
    	return R.ok().put("page", mpPageConvert.pageValueConvert(sysConfigList));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("course:acstatisticsuser:info")
    public R info(@PathVariable("id") Integer id){
        return R.ok().put("acStatisticsUser", acStatisticsUserService.getById(id));
    }


    /**
     * 新增用户统计表
     */
    @SysLog("新增用户统计表数据")
    @RequestMapping("/save")
    @RequiresPermissions("course:acstatisticsuser:save")
    public R save(@RequestBody AcStatisticsUser acStatisticsUser){
        acStatisticsUserService.save(acStatisticsUser);
        return R.ok();
    }


    /**
     * 修改
     */
    @SysLog("修改用户统计表数据")
    @RequestMapping("/update")
    @RequiresPermissions("course:acstatisticsuser:update")
    public R update(@RequestBody AcStatisticsUser acStatisticsUser){
		acStatisticsUserService.updateById(acStatisticsUser);
        return R.ok();
    }


    /**
     * 删除
     */
    @SysLog("删除用户统计表数据")
    @RequestMapping("/delete")
    @RequiresPermissions("course:acstatisticsuser:delete")
    public R delete(@RequestBody Integer[] ids){
		acStatisticsUserService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
    @GetMapping("exportExcel")
    @ApiOperation("导出excel")
   	public void exportExcel(HttpServletResponse response) {
   		List<AcStatisticsUser> acStatisticsAnswerList = acStatisticsUserService.list();
   		List<AcStatisticsUserExcelVO> acStatisticsAnswerVOList=mapperFacade.mapAsList(acStatisticsAnswerList, AcStatisticsUserExcelVO.class);
   		try {
   			EasyExcelUtil.exprotExcel(response, acStatisticsAnswerVOList, "用户统计表-" +DateUtil.localDateTimeFormat(LocalDateTime.now(), "yyyyMMdd"),
   			"用户统计" ,
   			AcStatisticsUserExcelVO.class);
   		} catch (Exception e) {
   			e.printStackTrace();
   		}
   	}
    @SysLog("清理统计数据")
    @RequestMapping("/clearStatisticsData")
    @RequiresPermissions("course:acstatisticsuser:delete")
    public R clearStatisticsData(){
    	acStatisticsUserService.clearStatisticsData();
    	return R.ok();
    }

    
    
}
